package cn.ffcs.uec.outStaff.action;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Messagebox;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.NodeVoUtil;
import cn.ffcs.uec.common.util.UecConstants;
import cn.ffcs.uec.outStaff.action.bean.UecOutStaffPartyLogEditBean;
import cn.ffcs.uec.outStaff.manager.UecOutStaffManager;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.recordsApproval.constant.ApprovalConstants;
import cn.ffcs.uec.recordsApproval.util.ApprovalUtil;
import cn.ffcs.uec.staff.model.UecPartyLog;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.PlatformUtil;
import cn.ffcs.uom.common.util.PubUtil;
import cn.ffcs.uom.common.util.StrUtil;

@Controller
@Scope("prototype")
public class UecOutStaffPartyLogEditComposer extends BasePortletComposer {

	private static final long serialVersionUID = 3086340593438520480L;
	/**
	 * 页面bean
	 */
	private UecOutStaffPartyLogEditBean bean = new UecOutStaffPartyLogEditBean();

	private UecOutStaffManager uecOutStaffManager = (UecOutStaffManager) ApplicationContextUtil
			.getBean("uecOutStaffManager");
	/**
	 * 操作类型
	 */
	private String opType;
	/**
	 * 人员信息
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 添加政治面貌
	 */
	private UecPartyLog uecPartyLog;

	private boolean isOutStaffEntryExt;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化窗口
	 * 
	 * @throws ParseException
	 */
	public void onCreate$uecPartyLogEditWindow() throws ParseException {
		this.bindCombobox();
		this.bindBean();
	}

	/**
	 * 绑定combobox
	 */
	private void bindCombobox() {
		// 政治面貌及党籍情况 partyName
		ListboxUtils.rendererForEdit(bean.getPartyName(),
				NodeVoUtil.getDictionaries(UecConstants.POLITICAL_OUTLOOK));

	}

	/**
	 * bindBean
	 * 
	 * @throws ParseException
	 */
	public void bindBean() throws ParseException {
		isOutStaffEntryExt = (Boolean) arg.get("isOutStaffEntryExt");
		opType = (String) arg.get("opType");
		if ("add".equals(opType)) {
			this.bean.getUecPartyLogEditWindow().setTitle("新增政治面貌信息");
			bean.getUecPartyLogEditDiv().setHeight("30%");
			uecOutStaffInfo = (UecOutStaffInfo) arg.get("uecOutStaffInfo");
		}
		if ("view".equals(opType)) {
			this.bean.getUecPartyLogEditWindow().setTitle("查看政治面貌信息");
			uecPartyLog = (UecPartyLog) arg.get("uecPartyLog");
			if (null != uecPartyLog) {
				PubUtil.fillBeanFromPo(uecPartyLog, bean);
				stringTransToDate();
				bean.getButtonDiv().setVisible(false);
				PubUtil.disableComponent(bean.getUecPartyLogEditDiv(), true);
			}
		}
		if ("edit".equals(opType)) {
			this.bean.getUecPartyLogEditWindow().setTitle("编辑政治面貌信息");
			bean.getUecPartyLogEditDiv().setHeight("30%");
			uecPartyLog = (UecPartyLog) arg.get("uecPartyLog");
			if (null != uecPartyLog) {
				PubUtil.fillBeanFromPo(uecPartyLog, bean);
				stringTransToDate();
			}
		}
		if ("approvalEdit".equals(opType)) {
			this.bean.getUecPartyLogEditWindow().setTitle("审批政治面貌信息修改");
			bean.getUecPartyLogEditDiv().setHeight("30%");
			uecPartyLog = (UecPartyLog) arg.get("uecPartyLog");
			if (null != uecPartyLog) {
				PubUtil.fillBeanFromPo(uecPartyLog, bean);
				stringTransToDate();
			}
		}
	}

	/**
	 * 保存
	 * 
	 * @throws Exception
	 */
	public void onOk() throws Exception {
		String msg = this.checkUacStaffData();
		if (null != msg) {
			Messagebox.show(msg);
			return;
		}
		String currentName = PlatformUtil.getCurrentUser().getScreenName();
		if ("add".equals(opType)) {
			uecPartyLog = UecPartyLog.newInstance();
			PubUtil.fillPoFromBean(bean, uecPartyLog);
			dateTransToString();
			uecPartyLog.setPkOrg(uecOutStaffInfo.getPkHrorg());
			uecPartyLog.setStaffId(uecOutStaffInfo.getOutStaffId());
			uecPartyLog.setDr(0L);
			uecPartyLog.setLastflag("Y");
			uecPartyLog.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			uecPartyLog.setPkPsndoc(uecOutStaffInfo.getPkPsnMsg());
			// 创建人
			uecPartyLog.setCreator(currentName);
			// 创建时间
			uecPartyLog.setCreationtime(DateUtil.getCurrenDate());
			if (!isOutStaffEntryExt) {
				// 发起流程
				if (ApprovalUtil.getActivityFlag()) {

					if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
						ZkUtil.showError("当前登录用户不存在!", "错误提示");
						return;
					}

					/**
					 * 数据入库 uec_partylog 添加一条记录
					 */
					Long outStaffId = uecOutStaffInfo.getOutStaffId();

					uecPartyLog.addStatusCdIS3();
					Long partyLogId = uecPartyLog.getPartylogId();

					String formTheme = "盟市外包人员政治面貌新增流程";
					String startFlowJson = "{" + "formTheme: '"
							+ formTheme
							+ "',"
							+ " formContent: '政治面貌新增人员:"
							+ uecOutStaffInfo.getPsnName()
							+ "',"
							+ " procDefId: '"
							+ ApprovalConstants.OUT_STAFF_PARTY_LOG_ADD_PROC_DEF_ID
							+ "'," + " urgent: '1'," + " atiActCategoryId: '"
							+ ApprovalConstants.MODIFICATION_CATEGORY_ID + "',"
							+ " procDefKey: '"
							+ ApprovalUtil.OUT_STAFF_PARTYLOG_ADD + "',"
							+ " formSender: '" + currentName + "',"
							+ " primaryId: '{" + "uec_out_staff_info_his:"
							+ outStaffId + ",uec_out_staff_info:" + outStaffId
							+ ",uec_partylog:" + partyLogId + "'}";

					String procInstId = null;
					try {
						procInstId = ApprovalUtil.startApproval(startFlowJson);
					} catch (Exception e) {
						// 流程发起失败 数据改为失效
						uecPartyLog.removeOnly();
						onCancel();
						ZkUtil.showError("流程发起失败！", "错误提示");
						e.printStackTrace();
					}
					if (StrUtil.isNullOrEmpty(procInstId)) {
						uecPartyLog.removeOnly();
						onCancel();
						ZkUtil.showError("流程发起失败！", "错误提示");
						return;
					}
					// 第一审批人如果是发起人直接到人力审批环节
					ApprovalUtil.isCurrentAssignNames(procInstId);
					ApprovalUtil.showNextAssignee(procInstId, formTheme);
				} else {
					// 直接修改
					uecPartyLog.addOnly();
					Messagebox.show("新增成功！");
				}
			}
			Events.postEvent("onOK", this.self, uecPartyLog);
			this.onCancel();
		}
		if ("edit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecPartyLog);
			dateTransToString();
			// 修改人
			uecPartyLog.setModifier(currentName);
			// 修改时间
			uecPartyLog.setModifiedtime(DateUtil.getCurrenDate());

			if (!isOutStaffEntryExt) {
				// 发起流程
				if (ApprovalUtil.getActivityFlag()) {

					if (StrUtil.isNullOrEmpty(PlatformUtil.getCurrentUser())) {
						ZkUtil.showError("当前登录用户不存在!", "错误提示");
						return;
					}
					// 判断该人员是否在审状态，如果是在审状态不允许再次发起此流程
					boolean isOnApproval = ApprovalUtil
							.isOnApproval(
									ApprovalConstants.OUT_STAFF_PARTY_LOG_EDIT_PROC_DEF_ID,
									uecPartyLog.getStaffId());
					if (isOnApproval) {
						ZkUtil.showError("在审状态不允许发起！", "错误提示");
						return;
					}
					/**
					 * 数据入库 uec_title 修改一条记录
					 */
					Long outStaffId = uecPartyLog.getStaffId();
					UecOutStaffInfo outStaffInfo = UecOutStaffInfo
							.newInstance();
					outStaffInfo.setOutStaffId(outStaffId);
					outStaffInfo = uecOutStaffManager
							.findUecOutStaffInfoById(outStaffInfo);

					Long partyLogIdHis = uecPartyLog.getPartylogId();

					uecPartyLog.addStatusCdIS3();
					Long partyLogId = uecPartyLog.getPartylogId();

					String formTheme = "盟市外包人员政治面貌修改流程";
					String startFlowJson = "{" + "formTheme: '"
							+ formTheme
							+ "',"
							+ " formContent: '政治面貌修改人员:"
							+ outStaffInfo.getPsnName()
							+ "',"
							+ " procDefId: '"
							+ ApprovalConstants.OUT_STAFF_PARTY_LOG_EDIT_PROC_DEF_ID
							+ "'," + " urgent: '1'," + " atiActCategoryId: '"
							+ ApprovalConstants.MODIFICATION_CATEGORY_ID + "',"
							+ " procDefKey: '"
							+ ApprovalUtil.OUT_STAFF_PARTYLOG_EDIT + "',"
							+ " formSender: '" + currentName + "',"
							+ " primaryId: '{" + "uec_out_staff_info_his:"
							+ outStaffId + ",uec_out_staff_info:" + outStaffId
							+ ",uec_partylog_his:" + partyLogIdHis
							+ ",uec_partylog:" + partyLogId + "'}";

					String procInstId = null;
					try {
						procInstId = ApprovalUtil.startApproval(startFlowJson);
					} catch (Exception e) {
						// 流程发起失败 数据改为失效
						uecPartyLog.removeOnly();
						onCancel();
						ZkUtil.showError("流程发起失败！", "错误提示");
						e.printStackTrace();
					}
					if (StrUtil.isNullOrEmpty(procInstId)) {
						uecPartyLog.removeOnly();
						onCancel();
						ZkUtil.showError("流程发起失败！", "错误提示");
						return;
					}
					// 第一审批人如果是发起人直接到人力审批环节
					ApprovalUtil.isCurrentAssignNames(procInstId);
					ApprovalUtil.showNextAssignee(procInstId, formTheme);
				} else {
					// 直接修改
					uecPartyLog.updateOnly();
					Messagebox.show("修改成功！");
				}
			}
			Events.postEvent("onOK", this.self, uecPartyLog);

			this.onCancel();
		}
		if ("approvalEdit".equals(opType)) {
			PubUtil.fillPoFromBean(bean, uecPartyLog);
			dateTransToString();
			// 修改人
			uecPartyLog.setModifier(currentName);
			// 修改时间
			uecPartyLog.setModifiedtime(DateUtil.getCurrenDate());

			uecPartyLog.updateOnly();
			Events.postEvent("onOK", this.self, uecPartyLog);

			this.onCancel();
			Messagebox.show("修改成功！");
		}
	}

	/**
	 * 取消
	 */
	public void onCancel() {
		// 关闭窗口
		this.bean.getUecPartyLogEditWindow().onClose();
	}

	/**
	 * 检测填入的参数是否为空
	 */
	private String checkUacStaffData() {
		if (StrUtil.isNullOrEmpty(bean.getPartyName().getSelectedItem().getValue())) {
			return "请填写政治面貌及党籍情况";
		}
		return null;
	}

	/**
	 * 将Date类型时间转成String类型 Date --> String 一套模型，两套标准：日期类型需要格式化
	 */
	public void dateTransToString() {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef1().getValue())) {
			uecPartyLog.setGlbdef1(formatter.format(bean.getGlbdef1()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getGlbdef2().getValue())) {
			uecPartyLog.setGlbdef2(formatter.format(bean.getGlbdef2()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getPartyDate().getValue())) {
			uecPartyLog.setPartyDate(formatter.format(bean.getPartyDate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getBeginDate().getValue())) {
			uecPartyLog.setBeginDate(formatter.format(bean.getBeginDate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getEndDate().getValue())) {
			uecPartyLog.setEndDate(formatter.format(bean.getEndDate()
					.getValue()));
		}
		if (!StrUtil.isNullOrEmpty(bean.getPartyDueDate().getValue())) {
			uecPartyLog.setPartyDueDate(formatter.format(bean.getPartyDueDate()
					.getValue()));
		}
	}

	/**
	 * 将String类型的时间转换成Date类型
	 * 
	 * @throws ParseException
	 */
	public void stringTransToDate() throws ParseException {
		// 时间类型转换：String --> Date
		// 奖励日期
		if (null != uecPartyLog.getGlbdef1()) {
			bean.getGlbdef1().setValue(
					DateUtil.patternStrToDate(uecPartyLog.getGlbdef1()));
		}
		if (null != uecPartyLog.getGlbdef2()) {
			bean.getGlbdef2().setValue(
					DateUtil.patternStrToDate(uecPartyLog.getGlbdef2()));
		}
		if (null != uecPartyLog.getPartyDate()) {
			bean.getPartyDate().setValue(
					DateUtil.patternStrToDate(uecPartyLog.getPartyDate()));
		}
		if (null != uecPartyLog.getBeginDate()) {
			bean.getBeginDate().setValue(
					DateUtil.patternStrToDate(uecPartyLog.getBeginDate()));
		}
		if (null != uecPartyLog.getEndDate()) {
			bean.getEndDate().setValue(
					DateUtil.patternStrToDate(uecPartyLog.getEndDate()));
		}
		if (null != uecPartyLog.getPartyDueDate()) {
			bean.getPartyDueDate().setValue(
					DateUtil.patternStrToDate(uecPartyLog.getPartyDueDate()));
		}

	}

}
